package com.example.demo.mqserver.core;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: 杰
 * Date: 2023 -07 -29
 * Time: 17:35
 */

import com.example.demo.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 该类表示一个存储消息的队列
 */
public class MSGQueue {
    // 队列的身份标识
    private String name;
    // 队列是否持久化 true 表示持久化, false 表示不持久化
    private boolean durable;
    // true 表示这个队列只能一个人使用, false 则是都可以使用. 暂时不实现
    private boolean exclusive;
    // true 表示没有人(消费者)使用之后自动删除, false 则不会自动删除. 暂时不实现
    private boolean autoDelete = false;
    // 扩展参数 . 暂不实现
    private Map<String,Object> arguments = new HashMap<>();
    // 当前队列都有那些消费者订阅了.
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    // 记录当前取到第几个消费者, 方便实现轮询策略.(使用 AtomicInteger 来保证原子性)
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    // 添加一个新的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv) {
        consumerEnvList.add(consumerEnv);
    }
    // 挑选一个订阅者, 来处理当前的消息 (按照轮询的方式)
    public ConsumerEnv chooseConsumer() {
        if (consumerEnvList.size() == 0) {
            // 该队列没有人订阅的.
            return null;
        }
        // 计算一下当前要取的元素的下标.
        int index = consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentJson, new TypeReference<Map<String, Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    // 提供一组 getter setter 用来进行测试
    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public void setArguments(String key, Object value) {
        arguments.put(key,value);
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }



}
